/*
 * simplelist.h
 *
 *  Created on: Feb 9, 2010
 *      Author: rohithdv
 */

#ifndef SIMPLELIST_H_
#define SIMPLELIST_H_

#include "node.h"
#include "utilityNode.h"
#include <iostream>
using namespace std;

template <class T>
class simplelist {
private:
	T *header;
	T *currPointer;
public:
	simplelist();
	~simplelist();
	int getCurrPointerValue();
	T* incrementCurrPointer();
	T* resetCurrPointer();
	T* incrementCurrPointer(int noofsteps);
	T* addNode(int *noofactions, double *utilityMatrix, int noofplayers);
	T* iterateAndGetValue(int noofstepsInSimpleList);
	void display();
};

template <class T>
T* simplelist<T>::iterateAndGetValue(int noofstepsInSimpleList) {
	resetCurrPointer();
	T* nodePtr = incrementCurrPointer(noofstepsInSimpleList);
	resetCurrPointer(); // reset currpointer back to header
	return nodePtr;
}

template <class T>
simplelist<T>::~simplelist() {
   cout << "SimpleList destructor called " << endl;
   T *temp = header;
   T *nextElement = NULL;
   while (temp){
	   nextElement = temp->getNextNode();
	   delete temp;
	   temp = nextElement;
   }
   cout << "utility node destructor was called " << utilityNode::numberOfTimesDestructorIsCalled << " number of times " << endl;
}

template <class T>
simplelist<T>::simplelist() {
	header = NULL;
	currPointer = NULL;
}

template <class T>
T* simplelist<T>::addNode(int *stratProfile, double *utilProfile, int noofplayers) {
	T *newNode = new T(stratProfile, utilProfile, noofplayers);
	if (header == NULL) {
		header = newNode;
	}
	else {
		T *tempPtr = header;
		while (tempPtr->getNextNode()) {
			tempPtr = tempPtr->getNextNode();
		}
		tempPtr->setNextPointer(newNode);
	}
	return newNode;
}

template <class T>
void simplelist<T>::display() {
	if (header == NULL) {
		cout << "Simple List is Empty " << endl ;
	}
	else {
		cout << "PRINTING CONTENTS OF SIMPLE LIST : START " << endl ;
		T *tempPtr = header;
		while (tempPtr) {
			tempPtr->display();
			tempPtr = tempPtr->getNextNode();
		}
		cout << endl << endl << "PRINTING CONTENTS OF SIMPLE LIST : END" << endl ;
	}
}

template <class T>
int simplelist<T>::getCurrPointerValue() {
	return currPointer->getValue();
}

template <class T>
T* simplelist<T>::incrementCurrPointer() {
	currPointer = currPointer->getNextNode();
	return currPointer;
}

template <class T>
T* simplelist<T>::incrementCurrPointer(int noofsteps) {
	for (int i=1; i<= noofsteps; i++) {
		currPointer = currPointer->getNextNode();
	}
	return currPointer;
}

template <class T>
T* simplelist<T>::resetCurrPointer() {
	currPointer = header;
	return header;
}


#endif /* SIMPLELIST_H_ */
